//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region Using directives

    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.DataLayer;
    using System.Globalization;

    #endregion

    /// <summary>
    /// Interaction logic for NamespaceDialog.xaml
    /// </summary>
    public partial class NamespaceDialog : Window
    {
        #region Private data

        /// <summary>
        /// Parent namespace.
        /// </summary>
        private Namespace parentNamespace;

        /// <summary>
        /// New namespace created.
        /// </summary>
        private Namespace newNamespace;

        /// <summary>
        /// Registry connection.
        /// </summary>
        private Connection registryConnection;

        #endregion

        #region Public properties

        /// <summary>
        /// Gets the namespace created.
        /// </summary>
        public Namespace Namespace
        {
            get
            {
                return this.newNamespace;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the Namespace dialog.
        /// </summary>
        public NamespaceDialog(Namespace parent, Connection connection)
        {
            InitializeComponent();

            this.parentNamespace = parent;
            this.registryConnection = connection;

            this.brdrHeader.MouseLeftButtonDown += (sender, e) => this.DragMove();
            this.txtName.TextChanged += new TextChangedEventHandler(this.OnNameChanged);
            this.btnSave.Click += new RoutedEventHandler(this.OnSaveNamespace);
            this.btnCancel.Click += new RoutedEventHandler(this.OnCancelNamespace);
            this.btnClose.Click += new RoutedEventHandler(this.OnCancelNamespace);
            this.txtName.Focus();
        }

        #endregion

        #region Private eventhandlers

        /// <summary>
        /// Fired when user changes category name. Checks if Save button has to be enabled or not.
        /// </summary>
        /// <param name="sender">
        /// Name textbox.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnNameChanged(object sender, TextChangedEventArgs e)
        {
            this.btnSave.IsEnabled = (this.txtName.Text.Trim().Length > 0);
        }

        /// <summary>
        /// Fired when user clicks save button.
        /// </summary>
        /// <param name="sender">
        /// Save button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnSaveNamespace(object sender, RoutedEventArgs e)
        {
            // Get the name entered.
            string name = this.txtName.Text.Trim();

            // Check if a namespace by this name already exists in the parent.
            Namespace child = this.parentNamespace.Children.FirstOrDefault(
                item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            if (child == null)
            {
                // If a namespace by this name does not already exist, create one.
                this.newNamespace = Namespace.Create(name, this.registryConnection);
                if (this.newNamespace != null)
                {
                    // Save the namespace.
                    this.newNamespace.Description = this.txtDescription.Text.Trim();
                    this.newNamespace.Parent = this.parentNamespace;
                    this.newNamespace.IsEditable = true;
                    this.newNamespace.Save();

                    // Close the window.
                    this.Close();
                }
            }
            else
            {
                // Namespace already exists. Report the error.
                string message = TridentResourceManager.GetString("NamespaceExistsError");
                TridentMessageBox.ShowTridentInformationMessageBox(this, string.Format(CultureInfo.InvariantCulture, message, name, this.parentNamespace.Name));
                this.txtName.Focus();
            }
        }

        /// <summary>
        /// Cancels the process.
        /// </summary>
        /// <param name="sender">
        /// Cancel button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnCancelNamespace(object sender, RoutedEventArgs e)
        {
            this.newNamespace = null;
            this.Close();
        }

        #endregion
    }
}
